﻿// E-Book Copier
// Copyright (C) 2009 Chuan-Gen Fan
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Project: http://code.google.com/p/ebookcopier/
// Contact Author:
//    website: http://chuangen.name/
// e-mail/msn: chuangen@live.cn

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace EbookCopier
{
    /// <summary>
    /// <para>透明窗体。你可以通过SetBitmap设置要显示的图像。</para>
    /// <para>
    /// 在将要设置的快捷菜单中，如果包含一项（可以是子菜单）Text 为 ReplaceMenuText，
    /// 则该项将被默认的工具菜单代替。否则，将添加到菜单的末尾。
    /// </para>
    /// </summary>
    internal partial class AlphaForm : Ref.PerPixelAlphaForm
    {
        /// <summary>
        /// 初始化类 <see cref="AlphaForm"/> 的新实例。
        /// </summary>
        public AlphaForm()
            :base()
        {
            this.TopMost = true;
            this.ShowInTaskbar = false;
        }


        #region 鼠标穿透

        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_STYLE = (-16);
        private const int GWL_EXSTYLE = (-20);
        private const int LWA_ALPHA = 0x2;

        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        private static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);


        private bool clickThroughEnable = false;
        /// <summary>
        /// 获取或设置一个值，指示窗体是否有鼠标穿透功能。
        /// </summary>
        /// <value>如果该实例can penetrate，为<c>true</c>；否则为<c>false</c>。</value>
        public bool ClickThroughEnable
        {
            get { return clickThroughEnable; }
            set
            {
                clickThroughEnable = value;

                if (clickThroughEnable)
                {//使窗口有鼠标穿透功能
                    uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
                    uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
                }
                else
                {//使窗体恢复正常
                    this.FormBorderStyle = FormBorderStyle.None;
                }
            }
        }
        #endregion

        /// <summary>
        /// 使窗口具有 WS_EX_LAYERED 扩展样式。
        /// CreateParams.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
        /// </summary>
        public static void SetAlphaStyle(Form form, bool isAlpha)
        {
            if (isAlpha)
            {
                uint intExTemp = GetWindowLong(form.Handle, GWL_EXSTYLE);
                uint oldGWLEx = SetWindowLong(form.Handle, GWL_EXSTYLE, intExTemp | WS_EX_LAYERED);
                //SetLayeredWindowAttributes(form.Handle, 0, 100, LWA_ALPHA);
            }
            else
            {//使窗体恢复正常
                form.FormBorderStyle = FormBorderStyle.None;
            }
        }
    }
}